package org.celery.modules.shopStatics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.celery.modules.shop.entity.Shop;
import org.celery.modules.shop.service.IShopService;
import org.celery.modules.shopStatics.entity.ShopStaticsDay;
import org.celery.modules.shopStatics.excel.ShopStaticsDayExcel;
import org.celery.modules.shopStatics.vo.ShopStaticsDayVO;
import org.celery.modules.shopStatics.mapper.ShopStaticsDayMapper;
import org.celery.modules.shopStatics.service.IShopStaticsDayService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺日数据统计表 服务实现类
 *
 * @author Celery
 * @since 2023-08-26
 */
@Service
@AllArgsConstructor
public class ShopStaticsDayServiceImpl extends BaseServiceImpl<ShopStaticsDayMapper, ShopStaticsDay> implements IShopStaticsDayService {

	private final IShopService shopService;

	@Override
	public IPage<ShopStaticsDayVO> selectShopStaticsDayPage(IPage<ShopStaticsDayVO> page, ShopStaticsDayVO shopStaticsDay) {
		return page.setRecords(baseMapper.selectShopStaticsDayPage(page, shopStaticsDay));
	}

	@Override
	public List<ShopStaticsDayExcel> exportShopStaticsDay(Wrapper<ShopStaticsDay> queryWrapper) {
		List<ShopStaticsDay> shopStaticsDayList = baseMapper.selectList(queryWrapper);
		List<Shop> shopList = shopService.list();
		List<ShopStaticsDayExcel> list = new ArrayList<>();
		shopStaticsDayList.forEach(shopStaticsDay -> {
			ShopStaticsDayExcel excelData = BeanUtil.copyProperties(shopStaticsDay, ShopStaticsDayExcel.class);
			shopList.forEach(shop -> {
				if (shop.getId().equals(shopStaticsDay.getShopId())) {
					excelData.setShopName(shop.getResourceOwner());
				}
			});
			list.add(excelData);
		});
		return list;
	}

	@Override
	public void importData(List<ShopStaticsDayExcel> list) {
		if (Func.isEmpty(list)) {
			return;
		}

		Date startDate = null;
		Date endDate = null;
		List<String> shopNameList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			ShopStaticsDayExcel item = list.get(i);
			if (i == 0) {
				startDate = DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT);
				endDate = DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT);
			}
			if (startDate.after(DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT))) {
				startDate = DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT);
			}
			if (endDate.before(DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT))) {
				endDate = DateUtil.parse(item.getDate(), DateUtil.DATE_FORMAT);
			}

			if (!shopNameList.contains(item.getShopName())) {
				shopNameList.add(item.getShopName());
			}
		}
		if (Func.isEmpty(shopNameList)) {
			return;
		}

		List<Shop> shopList = shopService.list(Wrappers.<Shop>lambdaQuery().in(Shop::getResourceOwner, shopNameList));
		if (Func.isEmpty(shopList)) {
			return;
		}

		List<ShopStaticsDay> shopStaticsDayList = list(Wrappers.<ShopStaticsDay>lambdaQuery()
				.in(ShopStaticsDay::getShopId, shopList.stream().map(Shop::getId).collect(Collectors.toList()))
				.between(ShopStaticsDay::getDate, startDate, endDate)
		);

		for (ShopStaticsDayExcel item: list) {
			Long shopId = null;
			for (Shop shop: shopList) {
				if (shop.getResourceOwner().equals(item.getShopName())) {
					shopId = shop.getId();
				}
			}

			// 未匹配到店铺
			if (Func.isEmpty(shopId)) {
				continue;
			}

			boolean match = false;
			for (ShopStaticsDay shopStaticsDay: shopStaticsDayList) {
				if (shopStaticsDay.getShopId().equals(shopId)) {
					match = true;
					break;
				}
			}

			ShopStaticsDay shopStaticsDay = BeanUtil.copyProperties(item, ShopStaticsDay.class);
			shopStaticsDay.setShopId(shopId);
			if (!match) {
				save(shopStaticsDay);
			} else {
				update(shopStaticsDay, Wrappers.<ShopStaticsDay>lambdaUpdate().eq(ShopStaticsDay::getShopId, shopId).eq(ShopStaticsDay::getDate, shopStaticsDay.getDate()));
			}
		}
	}

}
